package com.stylefeng.guns.modular.borrowing.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.aop.Platform;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.borrowing.service.ICustomerinfoService;
import com.stylefeng.guns.modular.system.model.Customerinfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 往来客户控制器
 *
 * @author fengshuonan
 * @Date 2018-08-06 14:31:19
 */
@Controller
@RequestMapping("/customerinfo")
public class CustomerinfoController extends BaseController {

    private String PREFIX = "/borrowing/customerinfo/";

    @Autowired
    private ICustomerinfoService customerinfoService;

    /**
     * 跳转到往来客户首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "customerinfo.html";
    }

    /**
     * 跳转到添加往来客户
     */
    @RequestMapping("/customerinfo_add")
    public String customerinfoAdd() {
        return PREFIX + "customerinfo_add.html";
    }

    /**
     * 跳转到修改往来客户
     */
    @RequestMapping("/customerinfo_update/{customerinfoId}")
    public String customerinfoUpdate(@PathVariable Integer customerinfoId, Model model) {
        Customerinfo customerinfo = customerinfoService.selectById(customerinfoId);
        model.addAttribute("item",customerinfo);
        LogObjectHolder.me().set(customerinfo);
        return PREFIX + "customerinfo_edit.html";
    }

    /**
     * 获取往来客户列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition, @Platform String platform) {
        //组装查询
        EntityWrapper<Customerinfo> wrapper = new EntityWrapper<Customerinfo>();
        wrapper.eq("platform",platform);
        if(StringUtils.isNotBlank(condition)) {
            wrapper.eq("customername", condition);
        }

        return customerinfoService.selectList(wrapper);
    }

    /**
     * 新增往来客户
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增往来客户")
    public Object add(Customerinfo customerinfo) {
        //组装查询
        EntityWrapper<Customerinfo> wrapper = new EntityWrapper<Customerinfo>();
        wrapper.eq("platform",ShiroKit.getUser().getDeptcode());
        wrapper.eq("customercode",customerinfo.getCustomercode());
        if(customerinfoService.selectCount(wrapper)>0){
            throw new GunsException(BizExceptionEnum.BUSINSS_ERROR, "已存在往来客户!");
        }
        customerinfo.setOperatordt( new Date());
        customerinfo.setOperator(ShiroKit.getUser().getName());
        customerinfoService.insert(customerinfo);
        return SUCCESS_TIP;
    }

    /**
     * 删除往来客户
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除往来客户")
    public Object delete(@RequestParam Integer customerinfoId) {
        customerinfoService.deleteById(customerinfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改往来客户
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改往来客户")
    public Object update(Customerinfo customerinfo) {
        customerinfo.setOperatordt( new Date());
        customerinfo.setOperator(ShiroKit.getUser().getName());
        customerinfoService.updateById(customerinfo);
        return SUCCESS_TIP;
    }

    /**
     * 往来客户详情
     */
    @RequestMapping(value = "/detail/{customerinfoId}")
    @ResponseBody
    public Object detail(@PathVariable("customerinfoId") Integer customerinfoId) {
        return customerinfoService.selectById(customerinfoId);
    }
}
